const config = require("./config");
var access_token = "";
const getoAuthToken = () => {
  if (access_token == "") {
    wx.request({
      url: "https://aip.baidubce.com/oauth/2.0/token",
      data: {
        'grant_type': 'client_credentials',
        'client_id': config.bd_API_Key,
        'client_secret': config.bd_Secret_Key,
      },
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        access_token = res.data.access_token;
        console.log('响应：', access_token);
        //   initQiniu(res.data)
      },
      fail: function (error) {}
    })
  }
}

function sumbitImgToBadu(data, onSuccess, onFailed) {
  var parm = data;
  parm.access_token = access_token;
  console.log("----", parm);
  wx.request({
    url: "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard",
    data: parm,
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    success: function (res) {
      console.log('ocr识别后的信息：', res); {
        onSuccess(res.data); //request success
      }
      //   initQiniu(res.data)
    },
    fail: function (error) {
      onFailed(error)
    }
  })
};
function queryYezz(data, onSuccess, onFailed) {
  var parm = data;
  parm.access_token = access_token;
  console.log("----", parm);
  wx.showLoading()
  wx.request({
    url: "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license",
    data: parm,
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    success: function (res) {
      console.log('ocr识别后的信息：', res); {
        onSuccess(res.data); //request success
      }
      //   initQiniu(res.data)
    },
    fail: function (error) {
      onFailed(error)
    },
    complete(){
      wx.hideLoading({
        success: (res) => {},
      })
    }
  })
};
module.exports = {
  getoAuthToken: getoAuthToken,
  sumbitImgToBadu: sumbitImgToBadu,
  queryYezz
}